package com.hong.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @Auther: Administrator
 * @Date: 2018/7/20 17:39
 * @Description:
 * 1.想让类的属性操作具备原子性
 * 1.1 volatile
 * 1.2非priveate，protected(如果是当前类也可以是private protected)
 * 1.3类型必须一致
 * 1.4其他
 */
public class AIFUTest {

    private volatile  int i;

    private AtomicIntegerFieldUpdater<AIFUTest> updater = AtomicIntegerFieldUpdater.newUpdater(AIFUTest.class,"i");

    public void update(int newValue){
        updater.compareAndSet(this,i,newValue);
    }

    public int get(){
        return  i;
    }

    public static void main(String[] args) {
        AIFUTest test  = new AIFUTest();
        test.update(10);
        System.out.println(test.get());
    }
}
